package com.qtyx.nms.mvp.presenter

import com.google.gson.Gson
import com.qtyx.nms.mvp.base.BasePresenter
import com.qtyx.nms.mvp.model.bean.*
import com.qtyx.nms.mvp.view.HomePageView
import com.qtyx.nms.util.ConstantUtil
import com.qtyx.nms.util.JsonUtil
import com.qtyx.nms.util.shared.AppInfoShared

/**
 * 测试数据
 */
class HomePageTestDataPresenter(val view: HomePageView) : BasePresenter() {

    /**一般护理 固定行数**/
    private val rowCountGeneral = 5

    /**特殊护理 固定行数**/
    private val rowCountSpecial = 6

    /**
     * 获取护理数据
     * @param type 1、一般护理，2、特殊护理
     * @param list 集合数据
     */
    private fun getNewList(type: Int, list: List<NurseBean.Data>): List<NurseBean.Data> {
        val rowCount = if (type == 1) rowCountGeneral else rowCountSpecial
        val spanCount = getSpanCount(type, list)
        val listNew = mutableListOf<NurseBean.Data>()
        listNew.addAll(list)
        //不足以铺满布局，添加空的对象
        if (list.size < rowCount * spanCount) {
            val differ = rowCount * spanCount - list.size
            for (i in 1..differ) {
                val data = NurseBean.Data("", "", "", "", listOf(), "", 0, 0)
                listNew.add(data)
            }
        }
        return listNew
    }

    /**
     * 获取护理数据的列数
     * @param type 1、一般护理，2、特殊护理
     * @param list 集合数据
     */
    private fun getSpanCount(type: Int, list: List<NurseBean.Data>): Int {
        val rowCount = if (type == 1) rowCountGeneral else rowCountSpecial
        val maxSpanCount = if (type == 1) 3 else 4

        var spanCount = if (list.size % rowCount > 0)
            list.size / rowCount + 1
        else
            list.size / rowCount

        if (spanCount > maxSpanCount) {
            spanCount = maxSpanCount
        }

        if (spanCount == 0) {
            spanCount = 1
        }
        return spanCount
    }


    /**
     * 获取护理数据
     */
    fun getNurseData() {
        val data = Gson().fromJson(
            JsonUtil.getAssetJson(ConstantUtil.jsonNursePath),
            NurseBean::class.java
        )
        AppInfoShared.saveHospitalName("医院名称")
        AppInfoShared.saveDeptName("科室名称")

        val listGeneral = data.data.filter { it.nursprogType == "1" }
        val listSpecial = data.data.filter { it.nursprogType == "2" || it.nursprogType == "3" }

        view.getGeneralNurseDataSucceed(getNewList(1, listGeneral), getSpanCount(1, listGeneral))
        view.getSpecialNurseDataSucceed(getNewList(2, listSpecial), getSpanCount(2, listSpecial))
    }

    /**
     * 获取护理等级颜色
     */
    fun getNurseGrade() {
        val data = Gson().fromJson(
            JsonUtil.getAssetJson(ConstantUtil.jsonNurseGradePath),
            NurseGradeBean::class.java
        )
        view.getNurseGradeSucceed(data.data)
    }

    /**
     * 获取值班信息
     */
    fun getZhiBanInfo() {
        val data =
            Gson().fromJson(JsonUtil.getAssetJson(ConstantUtil.jsonZhiBanPath), ZhiBanBean::class.java)
        view.getZhiBanInfoSucceed(data.data)
    }

    /**
     * 获取消息列表
     */
    fun getMessageList() {
        val data = Gson().fromJson(
            JsonUtil.getAssetJson(ConstantUtil.jsonMessagePath),
            MessageBean::class.java
        )
        view.getMessageSucceed(data.data)
    }

    /**
     * 获取医师列表
     */
    fun getYiShiList() {
        val data =
            Gson().fromJson(JsonUtil.getAssetJson(ConstantUtil.jsonYiShiPath), YiShiBean::class.java)

        val listNurse = mutableListOf<NurseBean.Data>()
        if (data.data.isNotEmpty()) {
            val map = mutableMapOf<String, MutableList<NurseBean.Data.SqlData>>()
            for (datum in data.data) {

                val sqlData = NurseBean.Data.SqlData(
                    datum.BED_NO,//床位号
                    datum.PATIENT_NAME,//病人姓名
                    datum.AGE,//年龄
                    datum.SEX,//性别
                    datum.DOCTOR_NAME,//主治医生
                    datum.NURSE_NAME,//责任护士
                    datum.CHARGE_TYPE,//收费种类
                    datum.BALANCE,//余额
                    datum.NURSE_LEVEL,//护理等级
                    datum.ADMISSION_DATE,
                    datum.BED_LABEL,
                    datum.DIAGNOSIS,
                    datum.INP_NO,
                    datum.MEAL,
                    datum.NATION
                )
                //主治医师ID
                if (map[datum.DOCTOR_IN_CHARGE] == null) {
                    val newList = mutableListOf<NurseBean.Data.SqlData>()
                    newList.add(sqlData)
                    map[datum.DOCTOR_IN_CHARGE] = newList
                } else {
                    map[datum.DOCTOR_IN_CHARGE]?.add(sqlData)
                }
            }

            for (values in map.values) {
                val yiShiInfo = "${values[0].DOCTOR_NAME}　　共 ${values.size} 人"
                val nurseData = NurseBean.Data(
                    yiShiInfo, "#FFFFFF",
                    "", "",
                    values, values[0].DOCTOR_NAME, 0, 0
                )
                listNurse.add(nurseData)
            }
        }
        view.getYiShiSucceed(listNurse)
    }
}